<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;

class UserResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
     */

    protected bool $showProtectedField = false;

    public function toArray($request)
    {
        if ($this->showProtectedField) return parent::toArray($request);

        $this->resource->makeHidden(['email','phone']);
        $data = parent::toArray($request);

        $data['fake_email'] = $this->resource->email? substr_replace($this->resource->email,'****',3,4):false;

        $data['fake_phone'] = $this->resource->phone? substr_replace($this->resource->phone,'****',3,4):false;

        return $data;
    }


    public function showProtectedFields()
    {
        $this->showProtectedField = true;
        return $this;
    }



}
